%PUT BUILDING TABLE 2 FOR &TITLE2....;

%MACRO SEQLISTEX(SPARM);	
	%LET UBOUND = &SPARM;	
	%LET SEQLIST_INDX = 2;

	/* INITIAL, NON-REPETITIVE CODE */
	TEST
	
	%DO %WHILE (&SEQLIST_INDX LE &UBOUND);
		/* REPETITIVE PORTION OF THE CODE */		
		XTIL&SEQLIST_INDX =
		
		%LET SEQLIST_INDX = %EVAL(&SEQLIST_INDX + 1);
	%END;

	/* FINAL CODE */
	0;
%MEND;
*;
* OUTPUT OUTLET MEANS FOR FULL MODEL REGRESSIONS;
*;
PROC MEANS DATA=OUT.&COM NOPRINT; BY OUTLET_CD;
	VAR LNPRICE &STDUM &RHYRS &RHS ;
	OUTPUT OUT=OMEANS(DROP=_TYPE_) 
	MEAN=MLNPRICE MN1-MN&NVEC N=COUNTER;
*;
TITLE "OUT.&COM RUNS";
*;
PROC MIXED DATA=OUT.&COM;
	MODEL LNPRICE=&RHYRS/SOLUTION;
	TITLE "TABLE 2 - UNADJUSTED INDEX";
	TITLE2 "&TITLE2";
	ODS OUTPUT SOLUTIONF=PDUMOLS(DROP=DF TVALUE PROBT RENAME=(ESTIMATE=IMPOLS));
*;
PROC MIXED DATA=OUT.&COM NOCLPRINT; CLASS QUOTESTRING;
	MODEL LNPRICE=&RHYRS QUOTESTRING/SOLUTION;
	TITLE "TABLE 2 - MATCHED MODEL INDEX";
	TITLE2 "&TITLE2";
	ODS OUTPUT SOLUTIONF=PDUMCPI(DROP=DF TVALUE PROBT RENAME=(ESTIMATE=IMPCPI));
*;
PROC MIXED DATA=OUT.&COM NOCLPRINT; CLASS QUOTESTRING; 
	MODEL LNPRICE=&RHYRS/SOLUTION;
	RANDOM QUOTESTRING;
	TITLE "SIMPLE HAUSMAN UNIT-VALUE MODEL GLS";
	TITLE2 "&TITLE2";
	ODS OUTPUT COVPARMS=COVNGLS SOLUTIONF=PDUMNGLS(DROP=DF TVALUE PROBT RENAME=(ESTIMATE=IMPNGLS));
*;
PROC MIXED DATA=OUT.&COM NOCLPRINT; CLASS OUTLET_CD;
	MODEL LNPRICE=&RHYRS &RHS 
	OUTLET_CD/SOLUTION;
	TITLE "TABLE 2 HEDONIC INDEX";
	TITLE2 "&TITLE2";
	ODS OUTPUT SOLUTIONF=PDUMFE(DROP=DF TVALUE PROBT RENAME=(ESTIMATE=IMPFE));
*;
PROC MIXED DATA=OUT.&COM NOCLPRINT; CLASS OUTLET_CD; 
	MODEL LNPRICE=&RHYRS &RHS /SOLUTION;
	RANDOM OUTLET_CD;
	TITLE "TABLE 2 HAUSMAN TEST. FULL RANDOM EFFECTS MODEL.";
	TITLE2 "&TITLE2";
	ODS OUTPUT COVPARMS=COVRE SOLUTIONF=PDUMRE(DROP=DF TVALUE PROBT RENAME=(ESTIMATE=IMPRE));
*;
PROC MIXED DATA=OUT.&COM ; 
	MODEL LNPRICE=&RHYRS &RHS /SOLUTION;
	TITLE "FULL MODEL WITH OLS";
	TITLE2 "&TITLE2";
	ODS OUTPUT SOLUTIONF=PDUMNOFE(DROP=DF TVALUE PROBT RENAME=(ESTIMATE=IMPNOFE));
*;
DATA VAR1A VAR2A; SET COVRE;
	IF COVPARM="OUTLET_CD" THEN OUTPUT VAR1A;
	ELSE OUTPUT VAR2A;
DATA VARSA(DROP=COVPARM); MERGE VAR1A(RENAME=(ESTIMATE=VARQ)) VAR2A(RENAME=(ESTIMATE=VARR));
DATA TEMP1A; IF _N_=1 THEN SET VARSA; SET OUT.&COM;
DATA WALDA; MERGE TEMP1A OMEANS; BY OUTLET_CD;
*;
*;
* SET DIMENSIONS BASED ON TIME PERIOD AND RIGHT HAND SIDE VARIABLES;
*;

	ARRAY PER &STDUM &RHYRS &RHS ;
	ARRAY XHAT XHAT1-XHAT&NVEC;
	ARRAY XTIL XTIL1-XTIL&NVEC;
	ARRAY QMEAN MN1-MN&NVEC;
	GAMMA=1-(VARR/(VARR+COUNTER*VARQ))**.5;
	INTHAT=1-GAMMA;
	YHAT=LNPRICE-GAMMA*MLNPRICE;
	DO JJ=1 TO &NVEC;
		XHAT(JJ)=PER(JJ)-GAMMA*QMEAN(JJ);
		XTIL(JJ)=PER(JJ)-QMEAN(JJ);
	END;
*;
*NEED TO EXTEND OR LIMIT LIST IN FIRST TEST STATEMENT;
*;
PROC REG DATA=WALDA;
	MODEL YHAT=INTHAT XHAT2-XHAT&NVEC/NOINT;
	MODEL YHAT=INTHAT XHAT2-XHAT&NVEC XTIL2-XTIL&NVEC/NOINT;
		%SEQLISTEX(&NVEC);
	MODEL YHAT=INTHAT XHAT2-XHAT&NVEC XTIL2-XTIL&NSPER/NOINT;
		%SEQLISTEX(&NSPER);
	TITLE "TABLE 2 VALUES HAUSMEN TEST - RANDOM EFFECTS VS. HEDONIC AKA WALD REGRESSIONS AND F TESTS ON FULL MODEL";
	TITLE2 "&TITLE2";
*;
* GET TIME DUMMY COEFFICIENTS FROM DIFFERENT MODELS;
*;
DATA MGOLS; SET PDUMOLS; 
	IF SUBSTR(EFFECT,1,2)="D0";
DATA MGCPI; SET PDUMCPI; 
	IF SUBSTR(EFFECT,1,2)="D0";
DATA MGNGLS; SET PDUMNGLS; 
	IF SUBSTR(EFFECT,1,2)="D0";
DATA MGNOFE; SET PDUMNOFE; 
	IF SUBSTR(EFFECT,1,2)="D0";
DATA MGFE; SET PDUMFE; 
	IF SUBSTR(EFFECT,1,2)="D0";
DATA MGRE; SET PDUMRE; 
	IF SUBSTR(EFFECT,1,2)="D0";
*;
*PICK UP OUTLET EFFECTS;
*;
DATA OUTSFE; SET PDUMFE; IF EFFECT="OUTLET_CD";
DATA MGOUT (KEEP=PERIOD OUTLET_CD STORECAT LGGROC DISCDEP--WAREHS OTHSTORE IMPFE); 
	MERGE OUT.&COM(KEEP=PERIOD OUTLET_CD STORECAT LGGROC DISCDEP--WAREHS OTHSTORE STORECAT)
  	OUTSFE; BY OUTLET_CD;
PROC SORT DATA=MGOUT; BY PERIOD;
*;
* GET MEAN EFFECTS BY STORECAT TO DECOMPOSE EFFECTS WITHIN AND WITHOUT;
*;
DATA MGOUTFL; SET MGOUT; IF PERIOD=&FIRSTPER OR PERIOD=200709;

PROC MEANS NOPRINT DATA=MGOUTFL; BY PERIOD; CLASS STORECAT;
	VAR IMPFE; OUTPUT OUT=IMPLIST MEAN=;
	TITLE "TABLE 6. OUTLET FIXED EFFECTS BY STORE CATEGORY";
	TITLE2 "&TITLE2";
PROC PRINT DATA=IMPLIST;
*;
* COMPUTE MEAN FIXED EFFECTS BY PERIOD;
*;

PROC MEANS NOPRINT DATA=MGOUT; BY PERIOD; VAR IMPFE; OUTPUT OUT=FEMEANS MEAN=MEANFE;
PROC PRINT DATA=FEMEANS;
	TITLE "MEAN OUTLET FIXED EFFECTS BY PERIOD";
	TITLE2 "&TITLE2";
*;
* REGRESS AVERAGE FIXED EFFECT ON TIME;
*;
DATA REG; SET FEMEANS;
	TIME=12*(ROUND(PERIOD/100)-2002)+MOD(PERIOD,100)-1;
*;
PROC REG S; MODEL MEANFE=TIME;
* TABLE 2, P VALUES FOR THE SIGNIFICANCE OF TIME TREND IN EXPLAINING OUTLET EFFECTS;
*;
* NORMALIZE AVERAGE FIXED EFFECT TO JAN 2002;
*;
*TABLE 2 - HEDONIC INDEX PLUS AVERAGE OUTLET PREMIUM;
DATA MGEFFS; SET FEMEANS;
	IF _N_=1 THEN BASEFE=MEANFE;
	RETAIN BASEFE;
	IF _N_=1 THEN DELETE;
	*AVGFE IS HE AVERAGE OUTLET PREMIUM;
	AVGFE=MEANFE-BASEFE;
*;
PROC PRINT;
TITLE "FIXED EFFECTS RELATIVE TO BASE PERIOD";
TITLE2 "&TITLE2";
*;
DATA MG (DROP=STDERR QUOTESTRING OUTLET_CD); MERGE MGOLS MGCPI MGNGLS MGNOFE 
	MGFE MGRE MGEFFS(KEEP=PERIOD AVGFE);
	*HEDONIC INDEX PLUS AVERAGE OUTLET PREMIUM;
	INDXO=IMPFE+AVGFE;
*;
*ENSURE INDEXES AND CONTRIBUTIONS DATA WRITTEN TO APPROPRIATE FILE;
*;
PROC EXPORT DATA=MG OUTFILE=OUTLOGS 
	DBMS=EXCEL REPLACE;
	SHEET=%LOOKUP(&COM);
RUN;
